package com.spic.business.mountguard.service.impl;

import com.spic.business.common.core.utils.ImageUtil;
import com.spic.business.enumconst.MsgLinkEnum;
import com.spic.business.mountguard.dto.QRCodeResultDTO;
import com.spic.business.mountguard.service.IMountGuardQrService;
import com.spic.common.core.text.UUID;
import com.spic.common.core.utils.SecurityUtils;
import com.spic.common.core.utils.SpringUtils;
import com.spic.common.redis.service.RedisService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 290512
 * @create 2021-11-01 10:47
 */
@Service
public class MountGuardQrServiceImpl implements IMountGuardQrService
{
    /**
     * 创建链接数据
     *
     * @param qrCodeResultDTO
     */
    @Override
    public Map<String,String> createAPPLink(QRCodeResultDTO qrCodeResultDTO)
    {
        Map<String,String> map = new HashMap<>();
        String loginDeptId = SecurityUtils.getDeptId()+"";
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String link = createAPPLink()+MsgLinkEnum.DEPTID.getValue()+loginDeptId+MsgLinkEnum.KEY.getValue()+uuid;
        //设置二维码链接缓存时间  3分钟过期后二维码链接失效
        String key = uuid;
        String binary = ImageUtil.creatRrCode(link, 200,200);
        SpringUtils.getBean(RedisService.class).setCacheObject(key,uuid,3L, TimeUnit.MINUTES);
        map.put("key",key);
        map.put("appLink",link);
        map.put("qrcode",binary);
        return map;
    }
    @Override
    public boolean selectQrCodeState(String key)
    {
        String loginDeptId = SecurityUtils.getDeptId()+"";
        //设置二维码缓存时间  3分钟过期后二维码链接失效
        Boolean state = SpringUtils.getBean(RedisService.class).hasKey(key);
        return state;
    }

    //二维码链接
    private String createAPPLink()
    {
        return MsgLinkEnum.REQUEST_HTTP.getValue()+MsgLinkEnum.BUSINESS_LIST.getValue()+MsgLinkEnum.MODULE_LIST.getValue();
    }
}
